package com.tengniu.comom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.biz.bo.LocationDataBO;
import com.tengniu.buss.domain.BaseLocation;
import com.tengniu.buss.service.BaseLocationService;
import com.tengniu.comom.service.DropdownService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DropdownServiceImpl implements DropdownService {

    @Resource
    private BaseLocationService baseLocationService;

    @Override
    public List<LocationDataBO> listProvinces() {
        List<BaseLocation> provinces = baseLocationService.list(Wrappers.lambdaQuery(BaseLocation.class)
                .eq(BaseLocation::getLevel, 1)
                .orderByAsc(BaseLocation::getId));
        return provinces.stream()
                .map(province -> {
                    LocationDataBO locationDataBO = new LocationDataBO();
                    locationDataBO.setCode(province.getCode());
                    locationDataBO.setName(province.getProvince());
                    return locationDataBO;
                }).collect(Collectors.toList());
    }

    @Override
    public List<LocationDataBO> listCities(String code) {
        List<BaseLocation> cities = baseLocationService.list(Wrappers.lambdaQuery(BaseLocation.class)
                .eq(BaseLocation::getLevel, 2)
                .likeRight(BaseLocation::getCode, code.substring(0, 2))
                .orderByAsc(BaseLocation::getId));
        return cities.stream()
                .map(city -> {
                    LocationDataBO locationDataBO = new LocationDataBO();
                    locationDataBO.setCode(city.getCode());
                    locationDataBO.setName(city.getCity());
                    return locationDataBO;
                }).collect(Collectors.toList());
    }

    @Override
    public List<LocationDataBO> listDistricts(String code) {
        List<BaseLocation> districts = baseLocationService.list(Wrappers.lambdaQuery(BaseLocation.class)
                .eq(BaseLocation::getLevel, 3)
                .likeRight(BaseLocation::getCode, code.substring(0, 4))
                .orderByAsc(BaseLocation::getId));
        return districts.stream()
                .map(district -> {
                    LocationDataBO locationDataBO = new LocationDataBO();
                    locationDataBO.setCode(district.getCode());
                    locationDataBO.setName(district.getDistrict());
                    return locationDataBO;
                }).collect(Collectors.toList());
    }

}
